﻿using _IMES.DBUtils;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using houseAgency.Base;
using houseAgency.Models;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace houseAgency.ViewModel.UserInfor
{
    public class PeopleInfoViewModel : ObservableObject
    {
        public PeopleInfoViewModel()
        {
			this.Title = Global.title ?? "";

            ID = GetUserIdByType();
        }

        #region 属性
        private string id;

        public string ID
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged();
            }
        }
        private string title;

		public string Title
		{
			get { return title; }
			set
			{
				title = value;
				OnPropertyChanged();
			}
		}

		private string name = "张三";

		public string Name
		{
			get { return name; }
			set
			{
				name = value;
                OnPropertyChanged();
			}
		}

        private string phone = "18834172334";

        public string Phone
        {
            get { return phone; }
            set
            {
                phone = value;
                OnPropertyChanged();
            }
        }

		private int sex = 0;

		public int Sex
		{
			get { return sex; }
			set
			{
                sex = value;
                OnPropertyChanged();
			}
		}

		private string homePhone = "0357-4585-063";

		public string HomePhone
        {
			get { return homePhone; }
			set
			{
				homePhone = value;
                OnPropertyChanged();
			}
		}

        private DateTime birthday = DateTime.Parse("1999-06-03");

        public DateTime Birthday
        {
            get { return birthday; }
            set
            {
                birthday = value;
                OnPropertyChanged();
            }
        }

        private string email = "1092794806@qq.com";

        public string Email
        {
            get { return email; }
            set
            {
                email = value;
                OnPropertyChanged();
            }
        }

        private string cardID = "123456789012345678";

        public string CardID
        {
            get { return cardID; }
            set
            {
                cardID = value;
                OnPropertyChanged();
            }
        }

        private bool oKButtonEnable = true;

        public bool OKButtonEnable
        {
            get { return oKButtonEnable; }
            set
            {
                oKButtonEnable = value;
                OnPropertyChanged();
            }
        }
        private bool houseButtonEnable = false;

        public bool HouseButtonEnable
        {
            get { return houseButtonEnable; }
            set
            {
                houseButtonEnable = value;
                OnPropertyChanged();
            }
        }
        #endregion


        #region 命令Command
        public ICommand OKCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    //MessageBox.Show(Name + Phone + Sex + HomePhone + Birthday + Email + CardID);
                    CheckInfo();
                    AddUserInfo();
                    Global.UserID = ID;
                });
            }
        }

        public ICommand HouseBtnCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    WindowManager.Show("House", null);
                });
            }
        }
        #endregion

        #region 私有方法
        private string GetUserIdByType()
        {
            string id = (Global.title == "出租人员信息设置" ? "lend" : "want");
            string sql = "select MAX(id) from user where type = @type";
            DataRow row = MySQLHelper.ExecuteDataRow(MySQLHelper.DBConnectionString, sql, new MySqlParameter("type",id));
            if (row != null && row.ItemArray[0].ToString() != "")
            {
                id += (int.Parse(row.ItemArray[0].ToString().Substring(4)) + 1).ToString();
            }
            else
            {
                id += 1001;
            }
            return id;
        }
        private void CheckInfo()
        {
            //校验输入的数据格式是否正确，可根据自己的需求修改
            string namePattern = @"^[\u4e00-\u9fa5]{2,10}$";  //是否是2到10位的汉字
            string phonePattern = @"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$";  //是否手机号码
            string homePhonePattern = @"^($$\d{3,4}-)|($$\d{3.4}-)?\d{7,8}$";  //是否是电话号码
            string cardIDPattern = @"^([0-9]){7,18}(x|X)?$ 或 ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$";  //是否是身份证号
            string emailPattern = @"^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$";  //是否是邮箱格式
            if (!IsMatch(Name, namePattern))
            {
                //不匹配执行的操作
            }
            if (!IsMatch(Phone, phonePattern))
            {
                //不匹配执行的操作
            }
            if (!IsMatch(HomePhone, homePhonePattern))
            {
                //不匹配执行的操作
            }
            if (!IsMatch(CardID, cardIDPattern))
            {
                //不匹配执行的操作
            }
            if (!IsMatch(Email, emailPattern))
            {
                //不匹配执行的操作
            }
        }

        private void AddUserInfo()
        {
            string sql = "insert into user(id,name,sex,birthday,phone,homePhone,email,cardID,type,recordDate) values(@id,@name,@sex,@birthday,@phone,@homePhone,@email,@cardID,@type,@recordDate)";
            int result = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] {
                    new MySqlParameter("id",ID), new MySqlParameter("name",Name), new MySqlParameter("sex",Sex == 0 ? "男" : "女"), new MySqlParameter("birthday",Birthday), new MySqlParameter("phone",Phone), new MySqlParameter("homePhone",HomePhone),
                    new MySqlParameter("email",Email), new MySqlParameter("cardID",CardID), new MySqlParameter("type",Global.title == "出租人员信息设置" ? "lend" : "want"), new MySqlParameter("recordDate",DateTime.Now)});
            if (result > 0)
            {
                OKButtonEnable = false;
                MessageBox.Show("已成功登记");
                if (Global.title == "出租人员信息设置")
                    HouseButtonEnable = true;
            }
        }

        #endregion


        #region 匹配方法
        /// <summary>  
        /// 验证字符串是否匹配正则表达式描述的规则  
        /// </summary>  
        /// <param name="inputStr">待验证的字符串</param>  
        /// <param name="patternStr">正则表达式字符串</param>  
        /// <returns>是否匹配</returns>  
        public static bool IsMatch(string inputStr, string patternStr)
        {
            return IsMatch(inputStr, patternStr, false, false);
        }

        /// <summary>  
        /// 验证字符串是否匹配正则表达式描述的规则  
        /// </summary>  
        /// <param name="inputStr">待验证的字符串</param>  
        /// <param name="patternStr">正则表达式字符串</param>  
        /// <param name="ifIgnoreCase">匹配时是否不区分大小写</param>  
        /// <param name="ifValidateWhiteSpace">是否验证空白字符串</param>  
        /// <returns>是否匹配</returns>  
        public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase, bool ifValidateWhiteSpace)
        {
            if (!ifValidateWhiteSpace && string.IsNullOrWhiteSpace(inputStr))//.NET 4.0 新增IsNullOrWhiteSpace 方法，便于对用户做处理
                return false;//如果不要求验证空白字符串而此时传入的待验证字符串为空白字符串，则不匹配  
            Regex regex = null;
            if (ifIgnoreCase)
                regex = new Regex(patternStr, RegexOptions.IgnoreCase);//指定不区分大小写的匹配  
            else
                regex = new Regex(patternStr);
            return regex.IsMatch(inputStr);
        }
        #endregion
    }
}
